import 'package:smart_freezer/core/utils/logs.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';
import 'package:smart_freezer/generated/locale_keys.g.dart';

class ProductModel {
  String id = "";
  String collectId = "";
  String icon = "";
  String code = "";
  String modelName = "";
  String name = "";
  String floorName = "";
  String hallName = "";
  /// 1在线, 2离线
  int networkStatus = 0;
  double temperatureOne = 0;
  double temperatureTwo = 0;
  /// 经销商名称
  String agencyName = "";
  /// 温控器型号
  String thermostatModel = "";
  /// WUBC3 / WUMC3
  String controllerModelName = "";
  /// 原厂保养期限
  String maintenanceTime = "";
  /// 记录/数据上传频率
  int uploadFrequency = 0;
  /// 设备共享人数
  int shareCount = 0;
  /// 序列号
  String serialNumber = "";

  /// 是否被选择
  bool isSelected = false;

  /// devId
  String devId = "";
  String customCode = "";  // 配置的数据点 code

  /// 产品ID
  String productId = "";

  /// T1探头故障: 0为正常, 大于0故障
  String t1Failure = "";
  /// T2探头故障: 0为正常, 大于0故障
  String t2Failure = "";
  /// T3探头故障: 0为正常, 大于0故障
  String t3Failure = "";
  /// 高温报警: 0为正常, 大于0报警
  String highTemperature = "";
  /// 低温报警: 0为正常, 大于0报警
  String lowerTemperature = "";
  /// 开门报警: 0为正常, 大于0报警
  String doorOpen = "";
  /// 冷凝器报警: 0为正常, 大于0报警
  String condenserClean = "";
  /// 报警信息
  String warningMessage = "";
  /// 温控器状态
  String controlStatus = "";

  /// 设备是否报警
  // String warnTag = "";

  /// alr 报警: 0-正常, 1-异常
  String alr = "";
  /// mute 报警消声: 0-正常, 1-异常
  String mute = "";
  /// def 除霜: 0-正常, 1-异常
  String def = "";
  /// door 开门: 0-正常, 1-异常
  String door = "";
  /// economy 经济模式: 0-正常, 1-异常
  String economy = "";

  ProductModel(
      {this.id = "",
        this.collectId = "",
        this.icon = "",
        this.code = "",
        this.modelName = "",
        this.name = "",
        this.floorName = "",
        this.hallName = "",
        this.networkStatus = 0,
        this.temperatureOne = 0,
        this.temperatureTwo = 0,
        /// 经销商名称
        this.agencyName = "",
        /// 温控器型号
        this.thermostatModel = "",
        this.controllerModelName = "",
        /// 原厂保养期限
        this.maintenanceTime = "",
        /// 记录/数据上传频率
        this.uploadFrequency = 0,
        /// 设备共享人数
        this.shareCount = 0,
        this.isSelected = false,
        this.serialNumber = "",
        this.devId = "",

        /// T1探头故障: 0为正常, 大于0故障
        this.t1Failure = "",
        /// T2探头故障: 0为正常, 大于0故障
        this.t2Failure = "",
        /// T3探头故障: 0为正常, 大于0故障
        this.t3Failure = "",
        /// 高温报警: 0为正常, 大于0报警
        this.highTemperature = "",
        /// 低温报警: 0为正常, 大于0报警
        this.lowerTemperature = "",
        // 开门报警: 0为正常, 大于0报警
        this.doorOpen = "",
        // 冷凝器报警: 0为正常, 大于0报警
        this.condenserClean = "",
        /// 报警信息
        this.warningMessage = "",
        /// 温控器状态
        this.controlStatus = "",

        /// alr 报警: 0-正常, 1-异常
        this.alr = "",
        /// mute 报警消声: 0-正常, 1-异常
        this.mute = "",
        /// def 除霜: 0-正常, 1-异常
        this.def = "",
        /// door 开门: 0-正常, 1-异常
        this.door = "",
        /// economy 经济模式: 0-正常, 1-异常
        this.economy = "",
      });

  ProductModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    collectId = json['collectId'] ?? "";
    icon = json['icon'] ?? "";
    code = json['code'] ?? "";
    devId = json["devId"] ?? "";
    modelName = json['modelName'] ?? "" ;
    name = json['name'] ?? "";
    floorName = json['floorName'] ?? "";
    hallName = json['hallName'] ?? "";
    networkStatus = json['networkStatus'] ?? 0;
    temperatureOne = json['temperatureOne'] ?? 0;
    temperatureTwo = json['temperatureTwo'] ?? 0;
    /// 经销商名称
    agencyName = json['agencyName'] ?? "";
    /// 温控器型号
    thermostatModel = json['thermostatModel'] ?? "";
    controllerModelName = json["controllerModelName"] ?? "";
    /// 原厂保养期限
    maintenanceTime = json['maintenanceTime'] ?? "";
    /// 记录/数据上传频率
    uploadFrequency = json['uploadFrequency'] ?? 0;
    /// 设备共享人数
    shareCount = json['shareCount'] ?? 0;
    /// 序列号
    serialNumber = json["serialNumber"] ?? "";

    /// T1探头故障: 0为正常, 大于0故障
    this.t1Failure = (json["t1Failure"] ?? 0) > 0 ? LocaleKeys.device_info_warn_t1.trc() : "";
    /// T2探头故障: 0为正常, 大于0故障
    this.t2Failure = (json["t2Failure"] ?? 0) > 0 ? LocaleKeys.device_info_warn_t2.trc() : "";
    /// T3探头故障: 0为正常, 大于0故障
    this.t3Failure = (json["t3Failure"] ?? 0) > 0 ? LocaleKeys.device_info_warn_t3.trc() : "";
    /// 高温报警: 0为正常, 大于0报警
    this.highTemperature = (json["highTemperature"] ?? 0) > 0 ? LocaleKeys.device_info_warn_up.trc() : "";
    /// 低温报警: 0为正常, 大于0报警
    this.lowerTemperature = (json["lowerTemperature"] ?? 0) > 0 ? LocaleKeys.device_info_warn_down.trc() : "";
    // 开门报警: 0为正常, 大于0报警
    this.doorOpen = (json["doorOpen"] ?? 0) > 0 ? LocaleKeys.device_info_warn_door.trc() : "";
    // 冷凝器报警: 0为正常, 大于0报警
    this.condenserClean = (json["condenserClean"] ?? 0) > 0 ? LocaleKeys.device_info_warn_lengningqi.trc() : "";

    // final warnList = [t1Failure, t2Failure, t3Failure, highTemperature, lowerTemperature, doorOpen, condenserClean];
    // warnList.forEach((element) {
    //   if(element.isNotEmpty){
    //     warningMessage += element + ", ";
    //   }
    // });
    // if(warningMessage.isNotEmpty){
    //   warningMessage = warningMessage.replaceRange(warningMessage.length - 2, warningMessage.length, "");
    // }

    logs("${devId}jsonwarningMessage: ${json["warningMessage"]}");
    warningMessage = json["warningMessage"] ?? "";

    warningMessage = warningMessage.isEmpty ? (networkStatus == 1 ? LocaleKeys.other_normal.trc() : LocaleKeys.other_lixian.trc())
                                              : warningMessage;
    // 在线才展示报警信息, 离线的设备状态为离线
    // warnTag =  networkStatus == 1 ? (warnInfo.isEmpty ? "0" : "1") : "";

    // 设置温控器状态
    /// 报警: 0为正常, 大于0异常
    this.alr = (json["alr"] ?? 0) > 0 ? LocaleKeys.device_info_wen_alr.trc() : "";
    /// 报警消声: 0为正常, 大于0异常
    this.mute = (json["mute"] ?? 0) > 0 ? LocaleKeys.device_info_wen_mute.trc() : "";
    /// Def: 0为正常, 大于0异常
    this.def = (json["def"] ?? 0) > 0 ? LocaleKeys.device_info_wen_def.trc() : "";
    /// 开门: 0为正常, 大于0异常
    this.door = (json["door"] ?? 0) > 0 ? LocaleKeys.device_info_wen_door.trc() : "";
    /// 经济模式: 0为正常, 大于0异常
    this.economy = (json["economy"] ?? 0) > 0 ? LocaleKeys.device_info_wen_economy.trc() : "";

    final wenList = [alr, mute, def, door, economy];
    wenList.forEach((element) {
      if(element.isNotEmpty){
        controlStatus += element + ", ";
      }
    });
    if(controlStatus.isNotEmpty){
      controlStatus = controlStatus.replaceRange(controlStatus.length - 2, controlStatus.length, "");
    }

    controlStatus = controlStatus.isEmpty ? LocaleKeys.other_normal.trc() : controlStatus;

  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['collectId'] = this.collectId;
    data['icon'] = this.icon;
    data['code'] = this.code;
    data['modelName'] = this.modelName;
    data['name'] = this.name;
    data['floorName'] = this.floorName;
    data['hallName'] = this.hallName;
    data['networkStatus'] = this.networkStatus;
    data['temperatureOne'] = this.temperatureOne;
    data['temperatureTwo'] = this.temperatureTwo;
    data["serialNumber"] = this.serialNumber;
    return data;
  }
}

